package com.hg.wap.service;


import java.util.HashMap;
import java.util.Map;

import com.hg.admin.vo.VO4User;
import com.hg.util.common.Common;
import com.hg.util.common.Sec;
import com.hg.wap.db.dao.UserDAO;
import com.hg.wap.db.dto.UserDTO;
import com.hg.wap.db.dto.UserLoginDTO;
import com.hg.wap.vo.VO4UserLogin;
import com.hg.wap.vo.VO4user;

public class UserService
{
	UserDAO dao = new UserDAO();
	
	public boolean ifRegistered(String phone){
		UserLoginDTO dto = dao.findByMobile(phone);
		if(dto!=null){
			return true;
		}
		return false;
	}
	
	public VO4UserLogin createUser(VO4UserLogin vo) {
			
		UserLoginDTO dto = new UserLoginDTO();
		dto.setUser_name(vo.getUser_name());
		dto.setUser_pwd(vo.getUser_pwd());
		dto.setEmail(vo.getEmail());
		dto.setMobile(vo.getMobile());
		dto.setByear(vo.getByear());
		dto.setBday(vo.getBday());
		dto.setLogin_ip(vo.getLogin_ip());
		dto.setCreate_time(Common.getTimeStamp());
		
		UserLoginDTO dtoL=dao.create(dto);
		return this.format(dtoL);
	}
	
	/**
	 * 自动创建用户+登录
	 * @param map	openid 肯定有      nickname sex headimgurl   当需要用户授权才用
	 * sex//用户的性别，值为1时是男性，值为2时是女性，值为0时是未知
	 * @return
	 */
	public Map<String, String> autoCreateLogin(Map<String, String> map){
		Map<String, String> userMap = new HashMap<String, String>();
		String openid = map.get("openid");
		userMap=dao.checkUser(openid);
		if("0".equals(userMap.get("err_code"))){
			//查不到用户,创建用户
			Map<String, String> daoMap = new HashMap<String, String>();
			daoMap.put("wx_openid", openid);
			
			String nickname = map.get("nickname");
			if(null==nickname || "".equals(nickname)){
				daoMap.put("user_name", openid);
			}else{
				daoMap.put("user_name", nickname);
			}
			
			String sex = map.get("sex");
			if(null==sex || "".equals(sex)){
				daoMap.put("sex", "0");
			}else{
				daoMap.put("sex", sex);
			}
			
			String headimgurl = map.get("headimgurl");
			if(null==headimgurl || "".equals(headimgurl)){
				daoMap.put("headimgurl", "");
			}else{
				daoMap.put("headimgurl", headimgurl);
			}
			daoMap.put("user_pwd", Sec.MD5Encrypt(openid, 32));
			daoMap.put("create_time", Common.getTimeStampStr());
			daoMap.put("update_time", Common.getTimeStampStr());
			
			userMap=dao.autoCreateUser(daoMap);
		}
		
		return userMap;
	}
	
	public VO4UserLogin login(VO4UserLogin vo) {
		UserLoginDTO dto = dao.findByMobile(vo.getUser_name());
		
		if(null==dto){
			return null;
		}else{
			return this.format(dto);
		}
	}
	
	/**
	 * 登录用
	 * @param openid
	 * @param user_id
	 */
	public VO4UserLogin getRow(String user_id){
		UserLoginDTO dto = dao.getRow(user_id);
		if(null==dto){
			return null;
		}else{
			return this.format(dto);
		}
	}
	
	/**
	 * 登录用
	 * @param openid
	 * @param user_id
	 */
	public VO4UserLogin getRow(String openid,String user_id){
		UserLoginDTO dto = dao.getRow(openid, user_id);
		if(null==dto){
			return null;
		}else{
			return this.format(dto);
		}
	}
	
	/**
	 * 业务层,格式化数据,dto对象转化成vo对象
	 * @param dto
	 * @return
	 */
	private VO4UserLogin format(UserLoginDTO dto){
		VO4UserLogin vo = new VO4UserLogin();
		vo.setFx_mall_bg(dto.getFx_mall_bg());
		vo.setIs_daren(dto.getIs_daren());
		vo.setVerify(dto.getVerify());
		vo.setYpoint(dto.getYpoint());
		vo.setScore(dto.getScore());
		vo.setFx_money(dto.getFx_money());
		vo.setFocus_count(dto.getFocus_count());
		vo.setBound_vip_num(dto.getBound_vip_num());
		vo.setLocate_time(dto.getLocate_time());
		vo.setRenren_id(dto.getRenren_id());
		vo.setT_name(dto.getT_name());
		vo.setIs_agent(dto.getIs_agent());
		vo.setProvince_id(dto.getProvince_id());
		vo.setT_openkey(dto.getT_openkey());
		vo.setWechat_id(dto.getWechat_id());
		vo.setAvatar(dto.getAvatar());
		vo.setIs_syn_sina(dto.getIs_syn_sina());
		vo.setM_left_times(dto.getM_left_times());
		vo.setUser_name(dto.getUser_name());
		vo.setTencent_id(dto.getTencent_id());
		vo.setReal_name(dto.getReal_name());
		vo.setIs_invite(dto.getIs_invite());
		vo.setSina_app_key(dto.getSina_app_key());
		vo.setReferer(dto.getReferer());
		vo.setUser_level(dto.getUser_level());
		vo.setSupplier_account_id(dto.getSupplier_account_id());
		vo.setByear(dto.getByear());
		vo.setInsite_count(dto.getInsite_count());
		vo.setLottery_verify(dto.getLottery_verify());
		vo.setIs_delete(dto.getIs_delete());
		vo.setPassword_verify(dto.getPassword_verify());
		vo.setLevel_id(dto.getLevel_id());
		vo.setTencent_app_secret(dto.getTencent_app_secret());
		vo.setDevice_token(dto.getDevice_token());
		vo.setMoney(dto.getMoney());
		vo.setMy_intro(dto.getMy_intro());
		vo.setXpoint(dto.getXpoint());
		vo.setFav_count(dto.getFav_count());
		vo.setFx_total_balance(dto.getFx_total_balance());
		vo.setTopic_count(dto.getTopic_count());
		vo.setIs_effect(dto.getIs_effect());
		vo.setVip_time(dto.getVip_time());
		vo.setPid4(dto.getPid4());
		vo.setPid3(dto.getPid3());
		vo.setPoint(dto.getPoint());
		vo.setPid6(dto.getPid6());
		vo.setPoster_url(dto.getPoster_url());
		vo.setPid5(dto.getPid5());
		vo.setPid8(dto.getPid8());
		vo.setPid7(dto.getPid7());
		vo.setFx_level(dto.getFx_level());
		vo.setOutsite_count(dto.getOutsite_count());
		vo.setPid9(dto.getPid9());
		vo.setPid2(dto.getPid2());
		vo.setPid1(dto.getPid1());
		vo.setIntegrate_id(dto.getIntegrate_id());
		vo.setIs_vip(dto.getIs_vip());
		vo.setTotal_score(dto.getTotal_score());
		vo.setT_openid(dto.getT_openid());
		vo.setUnionid(dto.getUnionid());
		vo.setDp_count(dto.getDp_count());
		vo.setBind_vip_num(dto.getBind_vip_num());
		vo.setLogin_time(dto.getLogin_time());
		vo.setTencent_app_key(dto.getTencent_app_key());
		vo.setLogin_pay_time(dto.getLogin_pay_time());
		vo.setLottery_mobile(dto.getLottery_mobile());
		vo.setGroup_id(dto.getGroup_id());
		vo.setAgent_id(dto.getAgent_id());
		vo.setCode(dto.getCode());
		vo.setFaved_count(dto.getFaved_count());
		vo.setLogin_ip(dto.getLogin_ip());
		vo.setSina_id(dto.getSina_id());
		vo.setQq_token(dto.getQq_token());
		vo.setIs_fx(dto.getIs_fx());
		vo.setReferral_count(dto.getReferral_count());
		vo.setFocused_count(dto.getFocused_count());
		vo.setUpdate_time(dto.getUpdate_time());
		vo.setEmail(dto.getEmail());
		vo.setCreate_time(dto.getCreate_time());
		vo.setWx_openid(dto.getWx_openid());
		vo.setMobile(dto.getMobile());
		vo.setUser_pwd(dto.getUser_pwd());
		vo.setArea_id(dto.getArea_id());
		vo.setSex(dto.getSex());
		vo.setSohu_id(dto.getSohu_id());
		vo.setT_access_token(dto.getT_access_token());
		vo.setMerchant_name(dto.getMerchant_name());
		vo.setIs_syn_tencent(dto.getIs_syn_tencent());
		vo.setId(dto.getId());
		vo.setCity_id(dto.getCity_id());
		vo.setBday(dto.getBday());
		vo.setSina_token(dto.getSina_token());
		vo.setQqv2_id(dto.getQqv2_id());
		vo.setUser_pic(dto.getUser_pic());
		vo.setDev_type(dto.getDev_type());
		vo.setIs_merchant(dto.getIs_merchant());
		vo.setSina_app_secret(dto.getSina_app_secret());
		vo.setBmonth(dto.getBmonth());
		vo.setPid(dto.getPid());
		vo.setLeft_times(dto.getLeft_times());
		vo.setIs_tmp(dto.getIs_tmp());
		vo.setFx_total_money(dto.getFx_total_money());
		vo.setVerify_create_time(dto.getVerify_create_time());
		vo.setDaren_title(dto.getDaren_title());
		vo.setKaixin_id(dto.getKaixin_id());
		vo.setStep(dto.getStep());
		return vo;
	}
	
	/*****************************************绑定上下3级*********开始*******************************/
	/**
	 * 当前用户
	 * @param id	当前用户
	 * @param pid	父ID
	 * @param pid1	父2级
	 * @param pid2	父3级
	 */
	public void bindingRelationship(int id,int pid,int pid1,int pid2){
		dao.bindingRelationship(id, pid, pid1, pid2);
	}
	/*****************************************绑定上下3级*********结束*******************************/
}
